package validates

import (
	"myapp/dal/model"
	"myapp/dal/query"
	"myapp/pkg"
	"strings"
	"time"
)

type Trend struct {
	ID      int32    `json:"id"`
	UserID  int32    `json:"user_id"`
	Images  []string `json:"images" binding:"required" label:"照片"`
	Content string   `json:"content" binding:"required" label:"内容"`

	Time         string `json:"time"`
	Nickname     string `json:"nickname"`
	Age          int    `json:"age"`
	City         string `json:"city"`
	Avatar       string `json:"avatar"`
	DatingType   string `json:"dating_type"`
	LikeCount    int32  `json:"like_count"`
	CommentCount int32  `json:"comment_count"`
	IsLiked      bool   `json:"is_liked"`
}

type Comment struct {
	ID      int32  `json:"id"`
	UserID  int32  `json:"user_id"`
	Content string `json:"content" binding:"required" label:"内容"`

	Time       string `json:"time"`
	Nickname   string `json:"nickname"`
	Age        int    `json:"age"`
	City       string `json:"city"`
	Avatar     string `json:"avatar"`
	DatingType string `json:"dating_type"`
}

// 处理函数：将 `Need` 转换为 `CustomNeed`，并添加额外字段
func ProcessTrend(nd *model.Trend, user_id int) Trend {
	images := strings.Split(nd.Images, ",")

	profile, _ := query.UserProfile.Where(query.UserProfile.UserID.Eq(nd.UserID)).Take()

	age := time.Now().Year() - int(profile.Birthday)
	// 返回自定义结构体
	time, _ := pkg.FormatTime(nd.CreatedAt.Format("2006-01-02 15:04:05"), false)

	is_liked := false
	if user_id > 0 {
		is_liked = true
	}

	return Trend{
		ID:           nd.ID,
		UserID:       nd.UserID,
		Content:      nd.Content,
		Time:         time,
		Images:       images,
		Nickname:     profile.Nickname,
		Age:          age,
		City:         profile.City,
		Avatar:       profile.Avatar,
		DatingType:   profile.DatingType,
		LikeCount:    nd.LikeCount,
		CommentCount: nd.CommentCount,
		IsLiked:      is_liked,
	}
}

func ProcessComment(nd *model.Comment, user_id int) Comment {
	profile, _ := query.UserProfile.Where(query.UserProfile.UserID.Eq(nd.UserID)).Take()

	age := time.Now().Year() - int(profile.Birthday)
	// 返回自定义结构体
	time, _ := pkg.FormatTime(nd.CreatedAt.Format("2006-01-02 15:04:05"), false)

	return Comment{
		ID:         nd.ID,
		UserID:     nd.UserID,
		Content:    nd.Content,
		Time:       time,
		Nickname:   profile.Nickname,
		Age:        age,
		City:       profile.City,
		Avatar:     profile.Avatar,
		DatingType: profile.DatingType,
	}
}
